import java.util.*;

/**
 * @author gao
 * @date 2025/10/12 19:04
 */

public class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();

        // 统计数字频率
        for (int i : nums) {
            map.put(i, map.getOrDefault(i, 0) + 1);
        }

        // 创建小根堆
        PriorityQueue<Integer> heap = new PriorityQueue<>(
                (a, b) -> map.get(a) - map.get(b)
        );

        // 维护堆的大小为 k
        for (int i : map.keySet()) {
            heap.offer(i);

            if (heap.size() > k) {
                heap.poll();
            }
        }

        // 取出堆中元素
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = heap.poll();
        }

        return ret;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < nums.length; i++) {
                nums[i] = in.nextInt();
            }
            int k = in.nextInt();
            Solution solution = new Solution();
            System.out.println(Arrays.toString(solution.topKFrequent(nums, k)));
        }
    }
}